package com.qolome.order.convert;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qolome.enums.ResultEnum;
import com.qolome.order.dto.request.CartRequestDto;
import com.qolome.order.dto.request.OrderForm;
import com.qolome.order.dto.request.OrderRequestDto;
import com.qolome.order.exception.OrderException;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Qolome
 * @version V1.0
 * @Description 转化类
 * @date 2020年 01月20日 10:11:55
 */
@Slf4j
public class OrderForm2OrderDTOConverter {
    public static OrderRequestDto convert(OrderForm orderForm) {
        Gson gson = new Gson();
        OrderRequestDto orderDTO = new OrderRequestDto();
        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerAddress(orderForm.getAddress());
        orderDTO.setBuyerOpenid(orderForm.getOpenid());

        List<CartRequestDto> orderDetailList = new ArrayList<>();
        try {
        	 orderDetailList = gson.fromJson(orderForm.getItems(),
                     new TypeToken<List<CartRequestDto>>() {
                     }.getType());
        } catch (Exception e) {
            log.error("【json转换】错误, string={}", orderForm.getItems());
            throw new OrderException(ResultEnum.PARAM_ERROR);
        }
        orderDTO.setCartRequestDtoList(orderDetailList);

        return orderDTO;
    }
}
